# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openxr-sdk
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.0.26
pkgrel=1
pkgdesc='OpenXR is a royalty-free, open standard that provides high-performance access to Augmented Reality (AR) and Virtual Reality (VR) (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.khronos.org/openxr/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('strip' '!debug' 'staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/KhronosGroup/OpenXR-SDK/archive/release-${pkgver}.tar.gz
        002-libname-def-file.patch
        003-disable-version-script.patch
        004-fix-cmake-install-dir.patch
        005-no-pdb-file-for-mingw-w64.patch
        libopenxr_loader_32.def)
sha256sums=('07e3e819af786c2a2bea16e8ec5243ce4e9d2876923112898fa4ea6dbd9bdd73'
            'da99914487a7c27137a13ff6ae39344cb9276a8cee41eeffc34b688deceff1a9'
            '66f71a382a27c82d17ea5425e61b18503cecc2a52b9b8c6561344d7a562c070e'
            'a03fc2adf3a3f083c92bb44e76511d22c6ad0ac5ab212ebce68e6dcbb01c72e7'
            '6acfe285e771e750fc2ac6dbcbf4142c3b7d86f660dd80e228ca4a2a90ff0e4a'
            'c268c94efe7ec8cfb83eccc506a8035869dd42eebfb5037537ecf642b08f25cc')
validpgpkeys=(F029455EAB70F520FF4A03BD7F534CD56F00321E)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}/OpenXR-SDK-release-${pkgver}

  apply_patch_with_msg \
    002-libname-def-file.patch \
    003-disable-version-script.patch \
    004-fix-cmake-install-dir.patch \
    005-no-pdb-file-for-mingw-w64.patch

  # For stdcall fixup, generated from upstream released DLLs
  if [ "${CARCH}" = "i686" ]; then
    cp ${srcdir}/libopenxr_loader_32.def src/loader/openxr-loader.def
  fi
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export VULKAN_SDK=${MINGW_PREFIX}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DDYNAMIC_LOADER=ON \
    -DBUILD_WITH_SYSTEM_JSONCPP=ON \
    ../OpenXR-SDK-release-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/OpenXR-SDK-release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
